import random
from time import sleep
from devices import dv
from tool.allure_ import allure_title
from tool.assertion import element_existence, element_info_existence, element_text_existence
from tool.dw import find_elements

'''这些是通过观察元素属性观察得出的结论，这些是通用值'''
def tu(number):
    '''家人时光每个图片定位'''
    tu = dv.xpath(
        f'//*[@resource-id="com.visteon.txzing.familytime:id/recycler_view"]/android.view.ViewGroup[{number}]/android.widget.ImageView[1]')
    return tu

# 设为按钮
set_as = dv(text='设为')
# 设置壁纸
set_wallpaper = dv.xpath('//*[@resource-id="com.visteon.txzing.familytime:id/check_wallpaper"]')
# 设置屏保
set_screensaver = dv.xpath('//*[@resource-id="com.visteon.txzing.familytime:id/check_screensaver"]')
# 设为按钮
confirm = dv.xpath('//*[@resource-id="com.visteon.txzing.familytime:id/btn_normal_allow"]')
# 取消按钮
cancel = dv.xpath('//*[@resource-id="com.visteon.txzing.familytime:id/btn_normal_cancel"]')

def set_s_w():
    allure_title('照片设置为屏保壁纸')
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/recycler_view"]/android.view.ViewGroup[1]/android.widget.ImageView[1]',
                  'xpath', '照片设置为屏保壁纸', '点击照片进入详情').click()

    find_elements(dv, '设为','text', '照片设置为屏保壁纸', '点击设为按钮').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/check_wallpaper"]',
                  'xpath', '照片设置为屏保壁纸', '勾选壁纸').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/check_screensaver"]',
                  'xpath', '照片设置为屏保壁纸', '勾选屏保').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/btn_normal_allow"]',
                  'xpath', '照片设置为屏保壁纸', '进行设置').click()
    # 多个断言处理
    ass_1 = element_text_existence(dv, '//*[@resource-id="com.visteon.txzing.familytime:id/tv_message"]', 'xpath',
                                   '壁纸、屏保设置成功','照片设置为屏保壁纸断言1')
    ass_2 = element_text_existence(dv, '//*[@resource-id="com.visteon.txzing.familytime:id/tv_label"]', 'xpath',
                                   '壁纸 屏保','照片设置为屏保壁纸断言2')
    ass_list = [ass_1, ass_2]
    for i in ass_list:
        if not i:
            print(f'断言{i}失败')
            return False
    return True

def cancel_s_w():
    allure_title('照片取消为屏保壁纸')
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/recycler_view"]/android.view.ViewGroup[1]/android.widget.ImageView[1]',
                  'xpath', '照片取消为屏保壁纸', '点击照片进入详情').click()

    find_elements(dv, '设为', 'text', '照片取消为屏保壁纸', '点击设为按钮').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/check_wallpaper"]',
                  'xpath', '照片取消为屏保壁纸', '取消勾选壁纸').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/check_screensaver"]',
                  'xpath', '照片取消为屏保壁纸', '取消勾选屏保').click()
    find_elements(dv,
                  '//*[@resource-id="com.visteon.txzing.familytime:id/btn_normal_allow"]',
                  'xpath', '照片取消为屏保壁纸', '进行设置').click()
    return element_text_existence(dv, '//*[@resource-id="com.visteon.txzing.familytime:id/tv_message"]', 'xpath',
                                  '壁纸、屏保取消成功','照片取消为屏保壁纸')
